﻿@COFE ?= {}

$ = jQuery
Entry = COFE.Entry
utils = COFE.utils

class Navigator extends Spine.Controller  
  constructor : () ->
    super
    @prevHistory = []
    # Warning: next is in opposite direction of prev.
    @nextHistory = [] 

  elements : 
    ".up"   : "up"
    ".back" : "back"
    ".next" : "next"

  events : 
    "click .home" : "goHome"
    "click .up" : "goUp"
    "click .back" : "goBack"
    "click .next" : "goForward"


  goHome : (event) =>
    @App.changePath("")

  goUp : (event) =>    
    @App.changePath(@parentPath, "Navigator", false)  
    

  goBack : (event) =>
    if @prevHistory.length > 0      
      @nextHistory.push(@App.path)      
      @App.changePath(@prevHistory.pop(), "Navigator", false)      
    
    #console.log @next @prev

  goForward : (event) =>
    if @next.length > 0      
      @prevHistory.push(@App.path)
      @App.changePath(@nextHistory.pop(), "Navigator", true)      
    

  init : ->    
    @App.bind("pathChanged", (sender, path) =>   
      if sender isnt "Navigator"
        if @path isnt undefined   
          @prevHistory.push(@path)        
        @nextHistory = []        
                    
      @path = path
      @parentPath = utils.extractFilepath(@path.param)
      
      if @parentPath isnt undefined then @up?.removeClass('ui-disabled')? else @up?.addClass('ui-disabled')?
      if @prevHistory.length > 0 then @back?.removeClass('ui-disabled')? else @back?.addClass('ui-disabled')?
      if @nextHistory.length > 0 then @next?.removeClass('ui-disabled')? else @next?.addClass('ui-disabled')?
      )
    ###
    @el.bind("swipeleft", =>
      @goNext())
    @el.bind("swiperight", => 
      @goBack())
    ###        
COFE.Navigator = Navigator